<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :title="title"
    width="800px">
    <dynForm
      ref="dataForm"
      :info-form="searchForm"
      :rules="rules"
      :select-list="selectList"
      :search-tags="searchTags"
      label-width="140px"/>
    <span slot="footer">
      <el-button icon="el-icon-circle-close" @click="handleClose">{{
        $t("cancel")
      }}</el-button>
      <el-button
        type="primary"
        icon="el-icon-receiving"

        @click="handleSave"
      >{{ $t('save') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { doAdd, doUpdate, getMaterialTypes, getSuppliers, getInfo } from '@/api/materialManagement/baseMaterial'
export default {
  name: 'BaseMaterialForm',
  data() {
    return {
      dialogVisible: false,
      title: '',
      searchForm: {},
      isEdit: false,
      searchTags:
        [
          { label: this.$t('materialCode'), prop: 'materialCode', tag: 'DynInput' },
          { label: this.$t('materialType'), prop: 'materialTypeID', tag: 'DynSelect' },
          { label: this.$t('supplierName'), prop: 'supplierID', tag: 'DynSelect' },
          { label: this.$t('purchaseOrderNo'), prop: 'purchaseOrder', tag: 'DynInput' },
          { label: this.$t('productionDate'), prop: 'productionDate', tag: 'Dyndate' },
          { label: this.$t('expireDate'), prop: 'expireDate', tag: 'Dyndate' },
          { label: this.$t('iniLength'), prop: 'iniLength', tag: 'DynInput', event: (value) => {
            this.searchForm.iniLength = value.replace(/[^\d.]/g, '')
          } },
          { label: this.$t('expireUseDays'), prop: 'expireUseDays', tag: 'DynInput', event: (value) => {
            this.searchForm.expireUseDays = value.replace(/[^\d]/g, '')
          } },
          { label: this.$t('effectiveTime'), prop: 'effectiveTime', tag: 'DynInput', event: (value) => {
            this.searchForm.effectiveTime = value.replace(/[^\d]/g, '')
          } },
          { label: this.$t('description'), prop: 'remark', tag: 'DynTextarea', row: 3, show_word_limit: true, maxlength: 200, width: '100%' }
        ],
      selectList: {},
      rules: Object.freeze(
        {
          materialCode: [{ required: true, message: `${this.$t('pleaseInput')}${this.$t('materialCode')}`, trigger: 'blur' }],
          materialTypeID: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('materialType')}`, trigger: 'change' }],
          productionDate: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('productionDate')}`, trigger: 'blur' }],
          expireDate: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('expireDate')}`, trigger: 'blur' }],
          iniLength: [{ required: true, message: `${this.$t('pleaseInput')}${this.$t('iniLength')}`, trigger: 'blur' }]
        }
      )
    }
  },
  methods: {
    handleOpen(row) {
      if (row && row.id) {
        this.title = this.$t('modify')
        this.$set(this.searchTags, 0, { label: this.$t('materialCode'), prop: 'materialCode', tag: 'DynInput', disabled: true })
        getInfo({ id: row.id }).then(res => {
          this.searchForm = res.data
        })
      } else {
        this.title = this.$t('add')
        this.$set(this.searchTags, 0, { label: this.$t('materialCode'), prop: 'materialCode', tag: 'DynInput', disabled: false })
      }
      this.dialogVisible = true
      this.getMaterialTypesList()
      this.getSuppliersList()
      this.$nextTick(() => {
        this.$refs['dataForm'].$refs['dynForm'].clearValidate()
      })
    },
    handleClose() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    handleUpdate() {
      this.$emit('initList')
    },
    handleSave() {
      this.$refs['dataForm'].$refs['dynForm'].validate((valid) => {
        if (valid) {
          if (this.searchForm.id) {
            doUpdate(this.$clearNull(this.searchForm)).then(res => {
              this.$betterTips(res, this.handleClose, this.handleUpdate)
            })
          } else {
            doAdd(this.$clearNull(this.searchForm)).then(res => {
              this.$betterTips(res, this.handleClose, this.handleUpdate)
            })
          }
        }
      })
    },
    // 获取材料类型
    async getMaterialTypesList() {
      const res = await getMaterialTypes()
      this.$set(this.selectList, 'materialTypeID', res.data)
    },
    // 获取供货商
    async getSuppliersList() {
      const res = await getSuppliers()
      this.$set(this.selectList, 'supplierID', res.data)
    }
  }
}
</script>

<style scoped lang="scss">
@import '~@/assets/styles/variables.scss';
::v-deep .el-form .el-form-item {
  width: 48%;
}
::v-deep .el-form .el-form-item:last-child .el-form-item__content{
  width: 60%;
}
::v-deep .el-date-editor {
  // 25是左边icon的宽
    width: $inputWidth - 4px;
}
</style>
